//
//  JKExt_Color.swift
//  Fanrengame
//
//  Created by 朱凯 on 2018/3/30.
//  Copyright © 2018年 JooKey. All rights reserved.
//

import Foundation
import UIKit

extension UIColor {

    var image: UIImage? {
        get {
            let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
            UIGraphicsBeginImageContextWithOptions(rect.size, false, 0)
            self.setFill()
            UIRectFill(rect)
            let image = UIGraphicsGetImageFromCurrentImageContext()
            return image
        }
    }
    
    convenience init(hexColor: String) {
        
        // 存储转换后的数值
        var red:UInt32 = 0, green:UInt32 = 0, blue:UInt32 = 0
        // 分别转换进行转换
        Scanner(string: String(hexColor[hexColor.startIndex...hexColor.index(hexColor.startIndex, offsetBy: 1)])).scanHexInt32(&red)
        Scanner(string: String(hexColor[hexColor.index(hexColor.startIndex, offsetBy: 2)...hexColor.index(hexColor.startIndex, offsetBy: 3)])).scanHexInt32(&green)
        Scanner(string: String(hexColor[hexColor.index(hexColor.startIndex, offsetBy: 4)...hexColor.index(hexColor.startIndex, offsetBy: 5)])).scanHexInt32(&blue)
        
        self.init(red: CGFloat(red)/255.0, green: CGFloat(green)/255.0, blue: CGFloat(blue)/255.0, alpha: 1.0)
    }
    
    
}
